package com.zyl.pagergallery_java.adapter;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.zyl.pagergallery_java.R;
import com.zyl.pagergallery_java.pojo.Pixabay;

import java.util.ArrayList;
import java.util.Arrays;

import io.supercharge.shimmerlayout.ShimmerLayout;

public class GalleryAdapter extends ListAdapter<Pixabay.PhotoItem, GalleryAdapter.GalleryViewHolder> {

    public GalleryAdapter() {
        super(new DiffUtil.ItemCallback<Pixabay.PhotoItem>() {
            @Override
            public boolean areItemsTheSame(@NonNull Pixabay.PhotoItem oldItem, @NonNull Pixabay.PhotoItem newItem) {
                return oldItem == newItem;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Pixabay.PhotoItem oldItem, @NonNull Pixabay.PhotoItem newItem) {
                return oldItem.getPhotoId() == newItem.getPhotoId();
            }
        });
    }

    @NonNull
    @Override
    public GalleryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_gallery, parent, false);

        final GalleryViewHolder galleryViewHolder = new GalleryViewHolder(inflate);

        galleryViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("PHOTO_LIST", new ArrayList(getCurrentList()));
                bundle.putInt("PHOTO_POSITION", galleryViewHolder.getAdapterPosition());
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_galleryFragment_to_photoFragment, bundle);
            }
        });

        return galleryViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final GalleryViewHolder holder, int position) {
        holder.shimmerLayoutGallery.setShimmerAngle(30);
        holder.shimmerLayoutGallery.setShimmerColor(0x55FFFFFF);
        holder.shimmerLayoutGallery.startShimmerAnimation();

        Glide.with(holder.itemView)
                .load(getItem(position).getPreviewUrl())
                .placeholder(R.drawable.ic_baseline_photo_size_select_actual_24)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        Log.e("myTAG", "onLoadFailed: ", e);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        holder.shimmerLayoutGallery.stopShimmerAnimation();
                        return false;
                    }
                })
                .into(holder.imageViewGallery);

    }

    class GalleryViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewGallery;
        ShimmerLayout shimmerLayoutGallery;

        public GalleryViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewGallery = itemView.findViewById(R.id.imageViewGallery);
            shimmerLayoutGallery = itemView.findViewById(R.id.shimmerLayoutGallery);
        }
    }
}
